frmDlgDemo Common Dialogs Demo wwwwwwwwwwwwww wwwwwwwwwwwwww wwwwwwwwwwwwww wwwwwwwwwwwwww wwwwwwwwwwwwww wwwww wwwwwww wwwwwww wwwwwww wwwwwww wwwwwww wwwywy wwwywwwyw wwwwywwwy wwwww wwwwww wwwww wwwwwwwwwwwwww wwwwwwwwwwwwww wwwwwwwwwwwwww wwwwwwwwwwwwww Form1, mnuDemo &Demo mnuFOpen File &Open... mnuFSave File &Save As... mnuFonts &Fonts... mnuPrinter &Printer mnuPSetup Printer Setup... mnuPDoc Print Document... mnuColors &Colors... mnuSep mnuExit E&xit mnuFonts_Click OldFontN frmDlgDemo FontName OldFontSize- FontSize OldFontWeight FontBold OldFontItalic FontItalic OldFontStrikethrux FontStrikethru% OldFontUnderlinex FontUnderline OldForeColor ForeColorn TChooseFont ChooseFont TLogFont LogFont Address MemHandleI GlobalAlloc GlobalLock lStructSize) hwndOwner hWnd; Flags CF_SCREENFONTSb CF_EFFECTS CF_LIMITSIZE nfonttype SCREEN_FONTTYPEC lpLogFont nSizeMinc nSizeMax2 Result hmemcpy{ GlobalUnlock GlobalFree lfFaceName NewFontNameo RTrimNulls NewFontSize iPointSize lfWeightF lfItalic lfStrikeOut lfUnderline rgbColorsV False mnuPSetup_Click TPrintDlg+ PrintDlg TDevMode DevMode TDevNames DevNames PD_PRINTSETUP PD_RETURNDCJ DeleteDC hDevNames szDriver szDevice szOutPut5 lstrcpy$ wDriverOffsetb wDeviceOffsetR wOutputOffsetf hDevModes dmDeviceName dmOrientation dmPaperSize dmScale mnuPDoc_ClickA nFromPage nToPage nMinPageq nMaxPage nCopies PD_SELECTION PD_PAGENUMS PD_COLLATE PD_PRINTTOFILE mnuColors_Click TChooseColord ChooseColork CustColorsArray{ wSize lpCustColors RgbResult BackColor CC_RGBINIT} CC_FULLOPEN mnuFOpen_ClickX TOpenFilename OpenFilename szFile szFilter szInitialDir szTitle? OFN_FILEMUSTEXISTT OFN_HIDEREADONLY OFN_PATHMUSTEXIST nFilterIndex lpstrFile nMaxFile lpstrFilter| lpstrInitialDirN lpstrTitle. GetOpenFileName nFileOffset nFileExtensionk mnuFSave_Click TSaveFilename NoTitle OFN_OVERWRITEPROMPT GetSaveFileName Form_Load Height Screen Width mnuExit_Click szString Demo for accessing Win 3.1 Common Dialogs Author: Costas Kitsos, CIS 73667,1755 Companion to WTJ ArticleI mnuFonts_Click Save the defaults so we can demo the newly selected font ChooseFont requires a pointer to a LogFont structure (TLogFont) Allocate a global block large enough to hold a LogFont structure always required Address for the LogFont structure Minimum font size we're willing to accept Maximum font size we're willing to accept make a local copy of ChooseFont's LogFont structure Free The Memory nothing to demo If the fontname string is empty then exit Common Dialogs ChooseFont"u Font: s Point Size:"s Weight:"s Boldu Weight:"s Normalu Italic:"s False"u Italic:"s Trueu StrikeOut:s False"u StrikeOut:s Trueu Underline:s False"u Underline:s Trueu select the new font print something restore the defaults mnuPSetup_Click Only show the Printer Setup Dialog Common Dialogs PrintDlg"u PrintDlg() returns an hDC, a global handle to hDevNames and another to hDevMode. Delete the ones we don't need since we're not going to print delete the hDC hDevNames should always be filled this works for old drivers as well - ones that don't support ExtDeviceModen lock the hDevNames handle make a local copy of hDevNamesw filename buffera device buffere port buffere Since the hDevNames only returns the offsets to NULL terminated strings we use the API lstrcpy which copies up to the terminating NULLM The string szDriver$ + szDevice$ + szOutPut$ can be used to updateN WIN.INI if our App needed to do so. Device:"s free the memory An hDevMode is not always returned, e.g. with old printer drivers.N Make a local copy of the global block (hDevMode)d free the memoryb Print some of the DevMode structure information to show that it worked. Printer:s Orientation:s Paper Size:"s (one of the DMPAPER_ constants)u Scale:s mnuPDoc_Click This is similar to Printer Setup but in this dialog the usero can select document setup options.u from page 1 to page 12 minimum page 1 maximum page 12 default number of copiesu since we're not printing delete the hDC Common Dialogs PrintDlg"u this works for old drivers as welle buffer to hold DRV filename buffer to hold Device namee buffer to hold Output Porte Since the hDevNames only returns the offsets to NULL terminated strings we use the API lstrcpy which copies up to the terminating NULL Device:"s Make a local copy of the DevMode structure Printer:s Find out what the user wanted to printt Print:s Selection"u From Page:s To Page:s Print"s All"u Did the user want copies collated? Print:s Collated copies"u Are we printing to a File?t Print:s Print to file"u Copies:"s mnuColors_Click Dimension a 16 element array to hold the Custom Colors A global block needs to be allocated to hold a copy of the custom colors Size of Memory Block Fill the Custom Colors Array with White Copy the Custom Colors Array to the global block Prepare to call ChooseColor Initialize to Background Colorc copy the new custom colors locallyg Free The Memoryo Print the RGB Resulte Common Dialogs ChooseColoru ChooseColor RGB Results Print the new custom colors Custom Colort mnuFOpen_Click Windows allows a maximum of about 8,000 selectors. To avoidu any problems, we allocate a big enough memory block to hold the necessary strings and sub-allocate it. buffer to hold filename Filters - note the double Chr$(0) at the endy VB Form Files (*.FRM)" *.frm" VB Module Files (*.BAS)" *.BAS" All Files (*.*)" Initial Directory is the current directory" show off the ability to customize the title of the dialog Common Dialogs - File Open In the Global Block, szFile$ is placed first, and then szFilter$d this makes it easy determining their Address since szFilter$ exists where szFile$ ends, at: Address + Len(szFile$)i Use the first filter Free The Memory Common Dialogs GetOpenFileNameu Selection: "s Path:"s Filename:"s Extension:s mnuFSave_Click This is similar to GetOpenFileName Windows allows a maximum of about 8,000 selectors. To avoid any problems, we allocate a big enough memory block to hold the necessary strings and sub-allocate it. untitled.txt suggest a filenameo buffer to hold the filename Filters - note the double Chr$(0) at the endh Visual Basic Files *.FRM;*.BAS;*.MAK" Plain Text Files *.TXT" Initial Directory is the current directoryA for the sake of the demo default to the second filter Free The Memory Common Dialogs GetSaveFileNameu Selection: "s Path:"s Filename:"s Extension:s Form_Load mnuExit_Click RTrimNulls Trims trailing Chr$(0)s